import { SQL_ERROR,QINIU } from '../server.config'
import db from '../utils/dbconf'
import qiniu from 'qiniu'
// import 
const { accessKey,secretKey,bucket } = QINIU
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const config = new qiniu.conf.Config()
const bucketManager = new qiniu.rs.BucketManager(mac, config)
export default{
  getToken:async ({query},res,next) => {
    try{
      const putPolicy = new qiniu.rs.PutPolicy({
        scope:bucket,
      })
      const token = putPolicy.uploadToken(mac);
      return res.send({code:200,data:{token}})
    }catch(e){
      console.log({e})
      return res.send({SQL_ERROR})
    }
  },
  deleteFile:async ({filename}) => {
    await bucketManager.delete(bucket, filename, (err) => {
      if (err) {
        console.log({err})
        throw new Error()
      }
    })
  }
}